//============================================================================
// Copyright (C) 2009-2013 Brett R. Jones 
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "VxNetLib.h"
#include "VxSktConnect.h"
#include <VxUtilLib/VxSktUtil.h>

// implements a manager to manage connect sockets
//============================================================================
//=== constructor ===//
VxSktConnectMgr::VxSktConnectMgr()
{
	m_eSktMgrType = eSktMgrTypeTcpConnect;
}

//============================================================================
//=== destructor ===//
VxSktConnectMgr::~VxSktConnectMgr()
{
}

//============================================================================
//! make a new socket... give derived classes a chance to override
VxSktBase * VxSktConnectMgr::MakeNewSkt( void )				
{ 
	return new VxSktConnect(); 
}

//============================================================================
//! Connect to ip or url and return socket.. if cannot connect return NULL
VxSktConnect * VxSktConnectMgr::ConnectTo(	const char *	pIpOrUrl,				// remote ip or url 
											U16				u16Port,				// port to connect to
											int				iTimeoutMilliSeconds)	// milli seconds before connect attempt times out
{
	if( NULL ==  m_pfnUserRecieve )
	{
		log_msg( 0, "VxSktConnectMgr::ConnectTo: you must call SetRecieveCallback first\n" );
		vx_assert( m_pfnUserRecieve );
	}

	VxSktConnect * poSkt = (VxSktConnect *)MakeNewSkt();
	poSkt->m_poSktMgr = this;
	poSkt->m_pfnRecieve = m_pfnOurRecieve;
	poSkt->m_pfnTransmit = m_pfnOurTransmit;
	RCODE rc = poSkt->ConnectTo(	m_oLclIp,
									pIpOrUrl, 
									u16Port, 
									iTimeoutMilliSeconds );
	if( rc )
	{
		log_msg( 0, "VxSktConnectMgr::ConnectTo: failed connect to %s %s\n", pIpOrUrl, DescribeSktErr( rc ) );
		delete poSkt;
		return NULL;
	}
	AddSkt( poSkt );
	return poSkt;
}

